package com.ygqh.baby.controller.mobile;

import com.ygqh.baby.ao.InformType;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResponseEntity;
import com.ygqh.baby.model.UserInformModel;
import com.ygqh.baby.po.YgInformUser;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgInformUserService;
import com.ygqh.baby.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @Author: sunshuo
 * @Date: 2019/4/9 12:28
 * @Version: 1.0
 */
@RestController
@RequestMapping("app/inform")
public class YgInformController extends BaseController {

	@Autowired
	private YgInformUserService ygInformUserService;

	/**
	 * 获取最新消息（未读）
	 *
	 * @return
	 */
	@RequestMapping("latestInform")
	public ResponseEntity latestInform() {
		YgUser currentUser = SessionUtil.getCurrentUser();
		UserInformModel userInformModel = ygInformUserService.findLatest(currentUser.getId());
		if (userInformModel != null && (userInformModel.getReadStatus()
				|| InformType.MemberChance.equals(userInformModel.getInformType()))) {
			userInformModel = null;
		}
		return ResponseEntity.success(userInformModel);
	}

	/**
	 * 消息列表
	 *
	 * @param queryInfo  分页
	 * @param readStatus 是否已读
	 * @return
	 */
	@RequestMapping("informList")
	public ResponseEntity informList(QueryInfo queryInfo, Boolean readStatus) {
		YgUser currentUser = SessionUtil.getCurrentUser();
		List<UserInformModel> inforList = ygInformUserService.
				findInformList(queryInfo, currentUser.getId(), readStatus);
		return ResponseEntity.success(inforList);
	}

	/**
	 * 标记消息已读
	 *
	 * @param id id
	 * @return
	 */
	@RequestMapping("readInform")
	public ResponseEntity readInform(@RequestParam Long id) {
		YgInformUser record = new YgInformUser();
		record.setId(id);
		record.setReadStatus(Boolean.TRUE);
		record.setReadTime(new Date());
		ygInformUserService.update(record);
		return ResponseEntity.success();
	}

	/**
	 * 全部标记为已读
	 *
	 * @return
	 */
	@RequestMapping("readAll")
	public ResponseEntity readAll() {
		ygInformUserService.updateByReadAll(SessionUtil.getCurrentUser().getId());
		return ResponseEntity.success();
	}
}
